package com.offcn.seckill.controller;

import com.offcn.seckill.pojo.SeckillOrder;
import com.offcn.seckill.service.SeckillOrderService;
import com.offcn.entity.PageResult;
import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.seckill.thread.SecKillOrderStatus;
import com.offcn.utils.IdWorker;
import com.offcn.utils.TokenDecode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:ujiuye
 * @Description:
 * @Date 2021/2/1 14:19
 *****/
@Api(tags = "SeckillOrderController")
@RestController
@RequestMapping("/seckillOrder")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;


    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private IdWorker idWorker;


    @RequestMapping("/add")
    public Result add(String time, Long id) {

        try {
            //获取登陆的用户名
            String username = tokenDecode.getinfo().get("username");
            Boolean add = seckillOrderService.add(id, time, username);

            if (add) {
                //抢购成功
                return new Result(true, StatusCode.OK, "抢购成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, e.getMessage());
        }
        return new Result(true, StatusCode.ERROR, "服务器繁忙,请稍后再试！！！");

    }

    @RequestMapping("/query")
    public Result queryStatus(){
        //获取用户名称
        String username = tokenDecode.getinfo().get("username");
        SecKillOrderStatus status = seckillOrderService.queryStatus(username);

        if (status!=null){
            return new Result(true,status.getStatus(),"抢购状态");
        }else {
            return new Result(false,StatusCode.NOTFOUNDERROR,"没有抢购信息");

        }
    }
}
